import 'package:flutter/material.dart';
import 'package:flutter_duqin/config/app_colors.dart';
import 'package:flutter_duqin/utils/user_type.dart';

class AvatarRoleName extends StatelessWidget {
  final String coverPictureUrl;
  final String nickname;
  final String type;
  final bool showType;
  final double avatarSize;

  const AvatarRoleName(
      {Key? key,
      required this.coverPictureUrl,
      required this.nickname,
      this.showType = false,
      this.type = "",
      this.avatarSize = 20})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        _avatar(),
        Offstage(
          offstage: !showType,
          child: _role(),
        ),
        _nickname(),
      ],
    );
  }

// 直径38，带有圆角
  Widget _avatar() {
    return SizedBox(
      width: avatarSize,
      height: avatarSize,
      child: ClipOval(
          child: FadeInImage.assetNetwork(
        placeholder: 'assets/images/common/lazy-1.png',
        image: coverPictureUrl,
      )),
    );
  }

  Widget _role() {
    return Container(
      margin: const EdgeInsets.only(left: 6),
      padding: const EdgeInsets.symmetric(vertical: 2, horizontal: 4),
      decoration: BoxDecoration(
          color: UserType.fromColor(type),
          borderRadius: BorderRadius.circular(4)),
      child: Text(
        UserType.fromCn(type),
        style: const TextStyle(color: Colors.white, fontSize: 10),
      ),
    );
  }

  Widget _nickname() {
    return Expanded(
        child: Container(
      margin: const EdgeInsets.only(left: 6),
      child: Text(
        nickname,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: const TextStyle(color: AppColors.unactive, fontSize: 14),
      ),
    ));
  }
}
